package com.meadvc.ssmdemo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.meadvc.ssmdemo.dao.ComponentDao;
import com.meadvc.ssmdemo.dao.SupplierDao;
import com.meadvc.ssmdemo.service.ComponentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: ssm-demo
 * @description:
 * @author: meadvc
 * @create: 2020-06-16 23:02
 **/
@Service
public class ComponentServiceImpl implements ComponentService {
    @Resource
    private ComponentDao dao;

    @Resource
    private SupplierDao supplierDao;
    @Override
    public JSONObject listComps(String query, int pagenum, int pagesize) {
        List<JSONObject> list = null;
        JSONObject meta = new JSONObject();
        JSONObject data = new JSONObject();
        //JSONObject comps = new JSONObject();
        JSONObject resultJson = new JSONObject();
        int offset = (pagenum-1)*pagesize;
        int total = 0;
        if(query==null||query.isEmpty()){
            list = dao.getComps(offset,pagesize);
            total = dao.countComps();

        }else {
            offset = 0;
            list = dao.getCompByName(query,offset,pagesize);
            pagenum =1;
            total = dao.countCompsByName(query);
        }
        if(list!=null){
            meta.put("msg","表单获取成功");
            meta.put("status",200);
        }else{
            meta.put("msg","表单获取失败");
            meta.put("status",400);
        }

        data.put("total",total);
        data.put("pagenum",pagenum);
        data.put("comps",list);
        resultJson.put("data",data);
        resultJson.put("meta",meta);
        return resultJson;
    }

    /**
     * 添加零件
     * @param requestJson
     * @return
     */
    @Override
    public JSONObject addComps(JSONObject requestJson) {
        JSONObject meta = new JSONObject();
        JSONObject data = new JSONObject();
        JSONObject resultJson = new JSONObject();
        int index = dao.addComp(requestJson);
        if(index!=0){
            meta.put("msg","零件添加成功");
            meta.put("status",200);
        }else{
            meta.put("msg","零件添加失败");
            meta.put("status",400);
        }
        data.put("index",index);
        resultJson.put("data",data);
        resultJson.put("meta",meta);
        return resultJson;
    }

    @Override
    public JSONObject updateComps(JSONObject requestJson) {
        JSONObject meta = new JSONObject();
        JSONObject data = new JSONObject();
        JSONObject resultJson = new JSONObject();
        int index = dao.updateComp(requestJson);
        if(index!=0){
            meta.put("msg","零件修改成功");
            meta.put("status",200);
        }else{
            meta.put("msg","零件修改失败");
            meta.put("status",400);
        }
        data.put("index",index);
        resultJson.put("data",data);
        resultJson.put("meta",meta);
        return resultJson;
    }

    @Override
    public JSONObject deleteComps(JSONObject requestJson) {
        JSONObject meta = new JSONObject();
        JSONObject data = new JSONObject();
        JSONObject resultJson = new JSONObject();
        int index = dao.removeComp(requestJson);
        if(index!=0){
            meta.put("msg","零件删除成功");
            meta.put("status",200);
        }else{
            meta.put("msg","零件删除失败");
            meta.put("status",400);
        }
        data.put("index",index);
        resultJson.put("data",data);
        resultJson.put("meta",meta);
        return resultJson;
    }

    @Override
    public JSONObject listSuppliers() {
        List<JSONObject> list = null;
        JSONObject meta = new JSONObject();
        JSONObject data = new JSONObject();
        JSONObject resultJson = new JSONObject();
        list = supplierDao.getSuppliers();
        if(list!=null){
            meta.put("msg","表单获取成功");
            meta.put("status",200);
        }else{
            meta.put("msg","表单获取失败");
            meta.put("status",400);
        }
        data.put("suppliers",list);
        resultJson.put("data",data);
        resultJson.put("meta",meta);
        return resultJson;

    }

    /**
     * 通过id获得零件
     * @param cid
     * @return
     */
    @Override
    public JSONObject getComps(int cid) {
        JSONObject json = null;
        JSONObject meta = new JSONObject();
        JSONObject data = new JSONObject();
        JSONObject resultJson = new JSONObject();
        json = dao.getCompById(cid);
        if(json!=null){
            meta.put("msg","表单获取成功");
            meta.put("status",200);
        }else{
            meta.put("msg","表单获取失败");
            meta.put("status",400);
        }
        data.put("comp",json);
        resultJson.put("data",data);
        resultJson.put("meta",meta);
        return resultJson;
    }

    /**
     * 获取所有的零件名称
     * @return
     */
    @Override
    public JSONObject listCompNames() {
        List<JSONObject> list = null;
        JSONObject meta = new JSONObject();
        JSONObject data = new JSONObject();
        JSONObject resultJson = new JSONObject();
        list = dao.getCompNames();
        if(list!=null){
            meta.put("msg","表单获取成功");
            meta.put("status",200);
        }else{
            meta.put("msg","表单获取失败");
            meta.put("status",400);
        }
        data.put("cnames",list);
        resultJson.put("data",data);
        resultJson.put("meta",meta);
        return resultJson;

    }

    @Override
    public JSONObject getCompsByName(String cname) {
        List<JSONObject> comps = dao.getCompByName2(cname);
        JSONObject meta = new JSONObject();
        JSONObject data = new JSONObject();
        JSONObject resultJson = new JSONObject();
        if(comps == null){
            meta.put("msg","表单获取失败");
            meta.put("status",400);
        }else{
            // 移动端获取某零件供应商信息
            int index = comps.size();
            List<JSONObject> list = new ArrayList<>();
            for(int i = 0;i < index; i++){
                JSONObject json = new JSONObject(comps.get(i));
                JSONObject jsonObject = new JSONObject();
                String s1 = (String) json.get("sname");
                String s2 = String.valueOf(json.get("price"));
                jsonObject.put("text",s1+"--单价:"+s2);
                jsonObject.put("value",s1);
                list.add(jsonObject);
            }

            meta.put("msg","表单获取成功");
            meta.put("status",200);
            data.put("comps",comps);
            data.put("cname",cname);
            data.put("list",list);
        }
        resultJson.put("data",data);
        resultJson.put("meta",meta);
        return resultJson;
    }
}
